/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.system.RoleOperRelaDao;
import org.grayrabbit.cms.entity.Role;
import org.grayrabbit.cms.entity.RoleOperRela;
import org.grayrabbit.cms.vo.system.RoleOperRelaBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>RoleOperRela</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 15:55:12 
 */
@Repository
public class RoleOperRelaDaoImpl extends GenericHibernateDao<RoleOperRela, String>
		implements RoleOperRelaDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From RoleOperRela as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see RoleOperRelaDao#assembly(RoleOperRelaBean)
	 */
	public QueryCondition assembly(RoleOperRelaBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see RoleOperRelaDao#assemblyCount(RoleOperRelaBean)
	 */
	public QueryCondition assemblyCount(RoleOperRelaBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see RoleOperRelaDao#count(RoleOperRelaBean)
	 */
	public int count(RoleOperRelaBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see RoleOperRelaDao#assemblyNameQuery(RoleOperRelaBean)
	 */
	public QueryCondition assermblyNameQuery(RoleOperRelaBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see RoleOperRelaDao#assemblyNameQueryCount(RoleOperRelaBean)
	 */
	public QueryCondition assemblyNameQueryCount(RoleOperRelaBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see RoleOperRelaDao#findByPage(Page, RoleOperRelaBean)
	 */
	public List<RoleOperRela> findByPage(Page<RoleOperRela> page,
			RoleOperRelaBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(RoleOperRelaBean bean,QueryCondition qc){
		if(bean!=null){
		}
	}

	public List<RoleOperRela> findAllAuthCodeByRoleId(String roleId) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(roleId)) 
			qc.add("And obj.role.roleId  = ?",roleId);
		return findByQc(qc);
	}

	public List<RoleOperRela> findObjectAuth(String targetId, String targetType) {
		if(StringUtils.isBlank(targetId) || StringUtils.isBlank(targetType)) 
			   throw new IllegalArgumentException(" targetId or targetType   can't be  blank!");
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.operTargetType = ? ", targetType);
		qc.append(" And ( ");
		qc.add(" obj.operTargetId like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId2 like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId3 like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId4 like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId5 like ?", "%"+targetId+"%");	
		qc.append(" ) ");
		return findByQc(qc);
	}
 
	public List<Role> findCategoryAuditorsRole(String codeId,
			String targetId) {
		
		if(StringUtils.isBlank(targetId) || StringUtils.isBlank(codeId)) 
			   throw new IllegalArgumentException(" targetId or codeId   can't be  blank!");
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.operCode.operId = ? ", codeId);
		qc.append(" And ( ");
		qc.add(" obj.operTargetId like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId2 like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId3 like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId4 like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId5 like ?", "%"+targetId+"%");	
		qc.append(" ) ");
		List<RoleOperRela> relaList = findByQc(qc);
		List<Role> roleList = new ArrayList<Role>();
		for(RoleOperRela roleoper:relaList){
			roleList.add(roleoper.getRole());
		}
		return roleList;
	}
 


}